/*
    Copyright 2007-2008 Adrien Guillon.  All Rights Reserved.

    This file is part of TBB Community Code. 

    TBB Community Code is free software; you can redistribute it
    and/or modify it under the terms of the GNU General Public License
    version 2 as published by the Free Software Foundation.

    TBB Community Code is distributed in the hope that it will be
    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with TBB Community Code; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    As a special exception, you may use this file as part of a free software
    library without restriction.  Specifically, if other files instantiate
    templates or use macros or inline functions from this file, or you compile
    this file and link it with other files to produce an executable, this
    file does not by itself cause the resulting executable to be covered by
    the GNU General Public License.  This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/


#ifndef __TCC_grouped_range_H
#define __TCC_grouped_range_H

// Use internal non-const version
#include "internal/nonconst_blocked_range.h"

#include "tbb/tbb_stddef.h"

namespace tcc {


/**
 * A grouped_range is a TBB-compatible Range which permits parallel
 * iteration over a container of grouped objects.  Note that an
 * iterator is provided here rather than a const_iterator because
 * the grouped items may or not be const, depending upon the type
 * which the user gave to the grouped items in the separator template
 * instantiation.
 */
template <typename GroupedContainer>
class grouped_range : public tcc::internal::blocked_range<typename GroupedContainer::iterator>
{
public:
    typedef tcc::internal::blocked_range<typename GroupedContainer::iterator> range_type;

    /**
     * Create a grouped_range which contains all grouped items in the container
     * provided. Optional grainsize. 
     */
    grouped_range(GroupedContainer& x, typename range_type::size_type grainsize=1) 
    : tcc::internal::blocked_range<typename GroupedContainer::iterator>(x.begin(), x.end(), grainsize)
    {
    }

    /**
     * Create a grouped_range which contains all grouped elements between a
     * provided begin and end iterator point, with an optional grainsize. 
     */
    grouped_range(typename GroupedContainer::iterator begin, typename GroupedContainer::iterator end, typename range_type::size_type grainsize=1) 
    : tcc::internal::blocked_range<typename GroupedContainer::iterator>(begin, end, grainsize)
    {
    }

    /**
     * Splitting constructor for TBB Range.
     */
    grouped_range(grouped_range& x, tbb::split y) : tcc::internal::blocked_range<typename GroupedContainer::iterator>(x, y)
    {
    }

    ~grouped_range() { }

private:
    grouped_range();
};

} // END tcc

#endif
